package com.yk.mr.service.job.impl;

import com.yk.mr.dao.common.api.ICommonDAO;
import com.yk.mr.dao.job.api.IJobDAO;
import com.yk.mr.model.file.FileModel;
import com.yk.mr.model.job.JobModel;
import com.yk.mr.service.file.api.IFileService;
import com.yk.mr.service.job.api.IJobService;
import org.hibernate.query.Query;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

@Service("jobService")
public class JobServiceImpl implements IJobService {
    @Resource(name = "jobDAO")
    private IJobDAO jobDAO;

    @Resource(name = "commonDAO")
    private ICommonDAO commonDAO;

    @Resource(name = "fileService")
    private IFileService fileService;

    @Override
    public boolean saveJob(String jobName, String jobType, String parameters, String jarPath, String jobInput, String jobOutput) {
        JobModel model = new JobModel();
        model.setJobName(jobName);
        model.setJobType(jobType);
        model.setJobParameters(parameters);
        model.setJarPath(jarPath);
        model.setJobInput(jobInput);
        model.setJobOutput(jobOutput);
        model.setCreateTime(new Timestamp(new Date().getTime()));
        model.setUpdateTime(new Timestamp(new Date().getTime()));
        return this.commonDAO.save(model);
    }

    @Override
    public boolean saveJob(String jobName, String jobType, String parameters) {
        JobModel model = new JobModel();
        model.setJobName(jobName);
        model.setJobType(jobType);
        model.setJobParameters(parameters);
        model.setJobInput(new JSONArray().toString());
        model.setJobOutput("");
        model.setCreateTime(new Timestamp(new Date().getTime()));
        model.setUpdateTime(new Timestamp(new Date().getTime()));
        return this.commonDAO.save(model);
    }

    @Override
    public boolean deleteJob(String jobId) {
        return this.commonDAO.delete(this.jobDAO.queryJobById(jobId));
    }

    @Override
    public boolean updateJob(String jobId, String input, String output) {
        JobModel model = this.getJobModel(jobId);
        FileModel fileModel = new FileModel();
        fileModel.setSize(0.0);
        fileModel.setCreateTime(new Timestamp(new Date().getTime()));
        fileModel.setUpdateTime(new Timestamp(new Date().getTime()));
        fileModel.setFileName(model.getJobName()+"目录");
        fileModel.setFilePath(output);
        fileModel.setRemark("MapReduce应用-"+model.getJobName()+"-数据目录");
        this.commonDAO.save(fileModel);
        model.setJobInput(input);
        model.setJobOutput(output);
        model.setUpdateTime(new Timestamp(new Date().getTime()));
        return this.commonDAO.update(model);
    }

    @Override
    public List<JobModel> getJobModel() {
        return this.jobDAO.queryAll();
    }

    @Override
    public List<JobModel> getJobModel(int start, int limit) {
        return this.jobDAO.queryPage(start,limit);
    }

    @Override
    public JobModel getJobModel(String jobId) {
        return this.jobDAO.queryJobById(jobId);
    }

    @Override
    public long querySum() {
        return 0;
    }

    @Override
    public Map<String, Object> getSelectFile(JSONArray selected) throws JSONException {
        List<FileModel> models=this.fileService.getFileModel();
        System.out.println(models);
        HashSet<String> selectedSet=new HashSet<>();
        for (int i = 0; i <selected.length() ; i++) {
            selectedSet.add(selected.getString(i));
        }
        List<FileModel> selectedList=new ArrayList<>();
        List<FileModel> unselectedList=new ArrayList<>();
        for(FileModel model:models){

            if(selectedSet.contains(model.getFileId())){
                selectedList.add(model);
            }else {
                unselectedList.add(model);
            }
        }
        Map<String,Object> result=new HashMap<>();
        result.put("selected",selectedList);
        System.out.println(selectedList);
        System.out.println(unselectedList);
        result.put("unselected",unselectedList);
        return result;
    }

}
